﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

using We.Moonlight.ViDesk;

namespace We.Moonlight.MSql
{
    public static class SqlViDesk
    {
        private static mDBTableAdapters.DesksTableAdapter DTA = new We.Moonlight.mDBTableAdapters.DesksTableAdapter();
        private static mDB.DesksDataTable DT = new mDB.DesksDataTable();

        public static PassiveDesk Select(int ID)
        {
            DTA.FillByID(DT, ID);
            if (DT.Rows.Count != 0)
            {
                return new PassiveDesk((mDB.DesksRow)DT.Rows[0]);
            }
            else
            {
                return null;
            }
        }
        public static List<PassiveDesk> Select()
        {
            DTA.Fill(DT);
            if (DT.Rows.Count != 0)
            {
                List<PassiveDesk> L = new List<PassiveDesk>();
                foreach (mDB.DesksRow DR in DT.Rows)
                {
                    L.Add(new PassiveDesk(DR));
                }
                return L;
            }
            else
            {
                return null;
            }
        }
        public static void Insert(PassiveDesk D)
        {
            DTA.Insert(D.Name, D.Icon.FullName, D.Background.FullName);
        }
        public static void Update(PassiveDesk D)
        {
            DTA.Update(D.Name, D.Icon.FullName, D.Background.FullName, D.ID);
        }
        public static void Update(PassiveDesk D, int ID)
        {
            DTA.Update(D.Name, D.Icon.FullName, D.Background.FullName, ID);
        }
        public static void Delete(PassiveDesk D)
        {
            Delete(D.ID);
        }
        public static void Delete(int ID)
        {
            switch (MessageBox.Show("Do you want to delete all Visual Documents in " + Select(ID).Name + "?", "Delete Visual Documents?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    SqlViItem.DeleteDesk(ID);
                    DTA.Delete(ID);
                    break;
                case DialogResult.Cancel:
                    return;
                default:
                    DTA.Delete(ID);
                    break;
            }
        }
    }
}